//
//  PickerViewViewController.m
//  PickerView
//

#import "PickerViewViewController.h"

NSMutableArray *comp1;
NSMutableArray *comp2;

@implementation PickerViewViewController

- (void)initComp1 {
	comp1 = [[NSMutableArray alloc] init];
	UIImageView *imgView;
	
	imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ca.png"]];
	[comp1 addObject:imgView];
	[imgView release];
	
	imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gb.png"]];
	[comp1 addObject:imgView];
	[imgView release];
	
	imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"us.png"]];
	[comp1 addObject:imgView];
	[imgView release];
	
	imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ko.png"]];
	[comp1 addObject:imgView];
	[imgView release];
}

- (void)initComp2 {
	comp2 = [[NSMutableArray alloc] init];
	UILabel *lbl;
	
	lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
	lbl.backgroundColor = [UIColor clearColor];
	lbl.text = @"Red";
	[comp2 addObject:lbl];
	[lbl release];
	
	lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
	lbl.backgroundColor = [UIColor clearColor];
	lbl.text = @"Blue";
	[comp2 addObject:lbl];
	[lbl release];
}

- (void)viewDidLoad {
    [super viewDidLoad];

	// 예제 데이터 생성
	[self initComp1];
	[self initComp2];
	
	CGRect pickerFrame = CGRectMake(0, 120, 0, 0);

	UIPickerView *myPicker = [[UIPickerView alloc]
							  initWithFrame:pickerFrame];
	myPicker.dataSource = self;
	myPicker.delegate = self;
	myPicker.showsSelectionIndicator = YES;
	
	[self.view addSubview:myPicker];
	
	[myPicker release];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
	if (component == 0) {
		return [comp1 count];
	} else {
		return [comp2 count];
	}

}

- (CGFloat)pickerView:(UIPickerView *)pickerView
	widthForComponent:(NSInteger)component {
	if (component == 0) {
		return 100.0;
	} else {
		return 200.0;
	}
}

- (UIView *)pickerView:(UIPickerView *)pickerView
			viewForRow:(NSInteger)row
		  forComponent:(NSInteger)component
		   reusingView:(UIView *)view {
	if (component == 0) {
		return [comp1 objectAtIndex:row];
	} else {
		return [comp2 objectAtIndex:row];
	}
}

- (void)pickerView:(UIPickerView *)pickerView
	  didSelectRow:(NSInteger)row
	   inComponent:(NSInteger)component {
	NSLog(@"row: %i, component: %i", row, component);
}

- (void)dealloc {
    [super dealloc];
}

@end
